home *** CD-ROM | disk | FTP | other *** search
/ Software Vault: The Gold Collection / Software Vault - The Gold Collection (American Databankers) (1993).ISO / cdr49 / 102_01.zip / PACUTILS.C < prev    next >
Text File  |  1993-06-03  |  9KB  |  423 lines

  1. #include "pacdefs.h"
  2. #include <bdscio.h>
  3.  
  4. char kbhit()
  5.  {    char c;
  6.     if ((inp(CSTAT) & CIMASK) == (CAHI ? CIMASK : 0))
  7.         switch(c = (inp(CDATA) & 0177))
  8.          {    case 'Q'-64:    Freeze = 0; break;
  9.             case 'S'-64:    Freeze = 1; break;
  10.             default:    InpChar = c;
  11.          }
  12.     return InpChar;
  13.  }
  14.  
  15. char getchar()
  16.  {    char c;
  17.     while (!(c = kbhit()));
  18.     InpChar = 0;    return c;
  19.  }
  20.  
  21. putchar(c)
  22.  char c;
  23.  {    do
  24.         kbhit();
  25.     while (Freeze || (inp(CSTAT) & COMASK) == (CAHI ? 0 : COMASK));
  26.     outp(CDATA, c); }
  27.  
  28. rawio()
  29.  {
  30.     InpChar = Freeze = 0;
  31.     rev = graph = 0;
  32.  }
  33.  
  34.  
  35. /* Initialize a board:                        */
  36.  
  37. in1(bd, y, cc)
  38.  char bd[BRDY][BRDX];
  39.  char *cc, y;
  40.  {    char x;
  41.     for (x=0; *cc; ) bd[y][x++] = *cc++;
  42.     bd[y][x] = 0;
  43.  }
  44.  
  45. initbd(bd)
  46.  char bd[BRDY][BRDX];
  47.  {    char i;
  48.     in1(bd, 0, "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
  49.     in1(bd, 1, "O + + + * + + + + OOO + + + + * + + + O");
  50.     in1(bd, 2, "O X OOO + OOOOO + OOO + OOOOO + OOO X O");
  51.     in1(bd, 3, "O * + + * + * + * + + * + * + * + + * O");
  52.     in1(bd, 4, "O + OOO + O + OOOOOOOOOOO + O + OOO + O");
  53.     in1(bd, 5, "O + + + * O + + + OOO + + + O * + + + O");
  54.     in1(bd, 6, "OOOOOOO + OOOOO + OOO + OOOOO + OOOOOOO");
  55.     in1(bd, 7, "      O + O + + * + + * + + O + O      ");
  56.     in1(bd, 8, "      O + O + OOO - - OOO + O + O      ");
  57.     in1(bd, 9, "OOOOOOO + O + O         O + O + OOOOOOO");
  58.     in1(bd, 10, "        * + * O         O * + *        ");
  59.     in1(bd, 11, "OOOOOOO + O + O         O + O + OOOOOOO");
  60.     in1(bd, 12, "      O + O + OOOOOOOOOOO + O + O      ");
  61.     in1(bd, 13, "      O + O * + + + + + + * O + O      ");
  62.     in1(bd, 14, "OOOOOOO + O + OOOOOOOOOOO + O + OOOOOOO");
  63.     in1(bd, 15, "O + + + * + * + + OOO + + * + * + + + O");
  64.     in1(bd, 16, "O X OOO + OOOOO + OOO + OOOOO + OOO X O");
  65.     in1(bd, 17, "O + + O * + * + * + + * + * + * O + + O");
  66.     in1(bd, 18, "OOO + O + O + OOOOOOOOOOO + O + O + OOO");
  67.     in1(bd, 19, "O + * + + O + + + OOO + + + O + + * + O");
  68.     in1(bd, 20, "O + OOOOOOOOOOO + OOO + OOOOOOOOOOO + O");
  69.     in1(bd, 21, "O + + + + + + + * + + * + + + + + + + O");
  70.     in1(bd, 22, "OOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO");
  71. }
  72.  
  73.  
  74. /* Some display utilities                    */
  75.  
  76. POS(row, col)
  77.  char row, col;
  78.  {    putchar(033); putchar('Y'); putchar(row+32); putchar(col+32);
  79.  }
  80.  
  81. PLOT(a, b, c)
  82.  {    POS(a, b);
  83.     putchar(c); }
  84.  
  85. SPLOT(a, b, c)
  86.  char *c;
  87.  {    POS(a, b);
  88.     while (*c) putchar(*c++); }
  89.  
  90. /* reverse video on/off:                */
  91.  
  92. revid(on)
  93.  char on;
  94.  {    if (on)
  95.      { if (!rev) { rev=1; printf("\033p"); }
  96.        return; }
  97.     if (rev) { rev=0; printf("\033q"); }}
  98.  
  99. /* graphics mode on/off:                */
  100.  
  101. graphics(on)
  102.  char on;
  103.  {    if (on)
  104.      { if (!graph) { graph=1; printf("\033F"); }
  105.        return; }
  106.     if (graph) { graph=0; printf("\033G"); }}
  107.  
  108.  
  109. /* Special version, for plotting the board:        */
  110.  
  111. BPLOT(a, b, c)
  112.  char *c;
  113.  {    char rev, ch;
  114.     rev = 0;
  115.     POS(a, b);
  116.     while (*c) switch(ch = *c++) {
  117.      case 'O':    graphics(0); revid(1); putchar(' '); continue;
  118.      default:    graphics(0); revid(0); putchar(ch); continue;
  119.      }
  120.     graphics(0);    revid(0);
  121.  }
  122.  
  123. update()
  124. {
  125.     POS(0, 52); printf("%6d", pscore);
  126.     POS(21, 57); printf("%6d", goldcnt);
  127. }
  128.  
  129. reinit()
  130. {
  131.     int locx, locy;
  132.     char tmp;
  133.  
  134.     for (locy = 0; locy < BRDY; locy++)
  135.     {
  136.         for (locx = 0; locx < BRDX; locx++)
  137.         {
  138.             tmp = initbrd[locy][locx];
  139.             brd[locy][locx] = tmp;
  140.             if ((display[locy][locx] = tmp) == CHOICE)
  141.             {
  142.                 display[locy][locx] = GOLD;
  143.             };
  144.         };
  145.     };
  146.     goldcnt = GOLDCNT;
  147.     delay -= (delay / 10);    /* hot it up */
  148. }
  149.  
  150. errgen(string)
  151. char    *string;
  152. {
  153.     SPLOT(23,45,string);
  154. }
  155.  
  156. dokill(mnum)
  157.     int mnum;
  158. {
  159.     struct pac *mptr;
  160.  
  161.     PLOT(0, 0, BEEP);
  162.     if (pacptr->danger == TRUE)
  163.     {
  164.         if (++killcnt == MAXMONSTER)
  165.         {
  166.             if (display[TRYPOS][TRXPOS] == GOLD)
  167.             {
  168.                 goldcnt--;
  169.             };
  170.             display[TRYPOS][TRXPOS] = TREASURE;
  171.             PLOT(TRYPOS, TRXPOS, TREASURE);
  172.             killcnt = 0;
  173.         };
  174.         SPLOT(5, 45, "MONSTERS KILLED: ");
  175.         POS(5, 62); printf("%1d", killcnt);
  176.         mptr = (&monst[mnum]);
  177.         mptr->ypos = MSTARTY;
  178.         mptr->xpos = MSTARTX + (2 * mnum);
  179.         mptr->stat = START;
  180.         PLOT(mptr->ypos, mptr->xpos, MONSTER);
  181.         pscore += KILLSCORE;
  182.         return(GOTONE);
  183.     };
  184.     return(TURKEY);
  185. }
  186.  
  187. /*
  188.  * clr -- issues an escape sequence to clear the display
  189. */
  190.  
  191. clr()
  192. {
  193.     putchar(033);    putchar('E');
  194. }
  195.  
  196. /*
  197.  *    display initial instructions
  198.  */
  199.  
  200. instruct()
  201. {
  202.     clr();
  203.     POS(0, 0);
  204.     printf("Attention: you are in a dungeon, being chased by monsters!\r\n\n");
  205.     printf("There are gold coins scattered uniformly in the dungeon, marked by \"+\".\r\n");
  206.     printf("One magic potion is available at each spot marked \"X\". Each potion will\r\n");
  207.     printf("enable you to kill monsters by touch for a limited duration. It will also\r\n");
  208.     printf("scare them away. When you kill a monster it is regenerated, but this takes\r\n");
  209.     printf("time. You can also regenerate yourself %d times. Killing all the monsters\r\n", MAXPAC);
  210.     printf("results in further treasure appearing magically somewhere in the dungeon,\r\n");
  211.     printf("marked by \"$\". There is a magic tunnel connecting the center left and\r\n");
  212.     printf("center right parts of the dungeon. The monsters know about it!\r\n\n");
  213.     printf("        Keypad: 4       to move left\r\n");
  214.     printf("                6       to move right\r\n");
  215.     printf("                8 or w  to move up\r\n");
  216.     printf("                2 or x  to move down\r\n");
  217.     printf("                5       to halt \r\n");
  218.     printf("                q       to quit\r\n\n");
  219.     printf("                1       normal game\r\n");
  220.     printf("                3       blinking monsters\r\n");
  221.     printf("                7       intelligent monsters\r\n");
  222.     printf("                9       blinking intelligent monsters\r\n");
  223. }
  224.  
  225. /*
  226.  * over -- game over processing
  227.  */
  228.  
  229. over()
  230. {
  231.     int i;
  232.     int line;
  233.     int scorefile;
  234.  
  235.  
  236.     poll(0);    /* flush and discard input from player */
  237.     clr();
  238.  
  239.     /* high score to date processing */
  240.     graphics(1);
  241.  
  242.     if (game != 0)
  243.     {
  244.         line = 7;
  245.         POS(line++, 20);
  246.         printf("faaaaaaaaaaaaaaaaaaaaaaaaaaac");
  247.         POS(line++, 20);
  248.         printf("`                           `");
  249.         POS(line++, 20);
  250.         printf("` G A M E   O V E R         `");
  251.         POS(line++, 20);
  252.         printf("`                           `");
  253.         POS(line++, 20);
  254.         printf("` \33GGame type: %1d\33F              `",game);
  255.  
  256.         POS(line++, 20);
  257.         printf("`                           `");
  258.         POS(line++, 20);
  259.         printf("` \33GYour score: %-5u\33F         `", pscore);
  260.         POS(line, 20);
  261.         printf("eaaaaaaaaaaaaaaaaaaaaaaaaaaad");
  262.     };
  263.     graphics(0);
  264.     leave();
  265. }
  266.  
  267. /*
  268.  * leave -- flush buffers,kill the Child, reset tty, and delete tempfile
  269.  */
  270.  
  271. leave()
  272. {
  273.     POS(23, 0);
  274.     exit(0);
  275. }
  276.  
  277. /*
  278.  * init -- does global initialization and spawns a child process to read
  279.  *      the input terminal.
  280.  */
  281.  
  282. init(argc, argv)
  283.  char **argv;
  284. {
  285.     int  *p, i;
  286.     unsigned tries;
  287.     char *arg;
  288.  
  289.     tries = 0;            /* previous initializations */
  290.     lastchar = DELETE;
  291.     initbd(initbrd);
  292.     initbd(brd);
  293.     initbd(display);
  294.     killcnt = 0;
  295.  
  296.     pacptr = &Pac;
  297.     pacsymb = PACMAN;
  298.  
  299.     pacstart.xpos =    PSTARTX;    pacstart.ypos = PSTARTY;
  300.     pacstart.dirn = DNULL;        pacstart.speed = SLOW;
  301.     pacstart.danger = FALSE;
  302.     pacstart.stat = 0;        /* ? */
  303.  
  304. /* Set up TTY stuff (deleted)            */
  305.  
  306.     rawio();
  307.  
  308.     /*
  309.      * New game starts here
  310.      */
  311.  
  312.     game = 0;
  313.     instruct();
  314.     while (game == 0)
  315.     {
  316.         poll(1);
  317.     };
  318.  
  319.     p = 0;            /* init random number generator    */
  320.     for (i=300; i--; tries += *p++);
  321.     srand(tries);
  322.  
  323.     delay = (1000 * CLOCKRATE);
  324.  
  325.     goldcnt = GOLDCNT;
  326.     pscore = 0;
  327.     clr();
  328.  
  329.     for (i=1; i<argc; i++) switch(*(arg = argv[i]))
  330.      { case 'D':    delay = atoi(++arg); continue;
  331.      }
  332.  
  333. }
  334.  
  335. /*
  336.  * poll -- read characters sent by input subprocess and set global flags
  337.  */
  338.  
  339. poll(sltime)
  340. {
  341.     int stop;
  342.     int charcnt;
  343.     int junk;
  344.  
  345.     stop = 0;
  346.  
  347.     while (kbhit()) switch(combuf[0] = 0177 & getchar())
  348.     {
  349.     case LEFT:
  350.         pacptr->dirn = DLEFT;
  351.         break;
  352.  
  353.     case RIGHT:
  354.         pacptr->dirn = DRIGHT;
  355.         break;
  356.  
  357.     case NORTH:
  358.     case NNORTH:
  359.         pacptr->dirn = DUP;
  360.         break;
  361.  
  362.     case DOWN:
  363.     case NDOWN:
  364.         pacptr->dirn = DDOWN;
  365.         break;
  366.  
  367.     case HALT:
  368.         pacptr->dirn = DNULL;
  369.         break;
  370.  
  371.     case ABORT:
  372.     case DELETE:
  373.     case QUIT:
  374.     case CONTROL_C:
  375.         over();
  376.         break;
  377.  
  378.     case CNTLS:
  379.         stop = 1;
  380.         continue;
  381.  
  382.     case GAME1:
  383.         game = 1;
  384.         break;
  385.  
  386.     case GAME2:
  387.         game = 2;
  388.         break;
  389.  
  390.     case GAME3:
  391.         game = 3;
  392.         break;
  393.  
  394.     case GAME4:
  395.         game = 4;
  396.         break;
  397.  
  398.     default:
  399.         continue;
  400.     }
  401. }
  402.  
  403. int getrand(range)
  404. {
  405.     return rand() % range;
  406. }
  407.  
  408.  
  409. /* dfp - real function for tputs function to use */
  410. putch( ch )
  411.     int ch;
  412. {
  413.     putchar( ch );
  414. }
  415.  
  416. /* saw -- a fake.                */
  417.  
  418. sleep(secs)
  419.  {    int i;
  420.     while (secs-- > 0)
  421.       for (i=(1500 * CLOCKRATE); i--; );
  422.  }
  423.